﻿@page "/SysClient"
@using SelfService.ViewModel.SysClientVMs;
@using SelfService.Model.Models
@using SelfService.ViewModel.SysClientTaskVMs
@inherits BasePage
@attribute [ActionDescription("客户端管理", "SelfService.Controllers,SysClient")]

<WTSearchPanel OnSearch="@DoSearch">
    <ValidateForm Model="@SearchModel">
        <Row ItemsPerRow="ItemsPerRow.Three" RowType="RowType.Inline">

            <BootstrapInput @bind-Value="@SearchModel.Name"  />
            <BootstrapInput @bind-Value="@SearchModel.Code"  />
            <BootstrapInput @bind-Value="@SearchModel.Address"  />
            <BootstrapInput @bind-Value="@SearchModel.Ip"  />
            <BootstrapInput @bind-Value="@SearchModel.MacAddress"  />
            <BootstrapInput @bind-Value="@SearchModel.Version"  />
            <MultiSelect @bind-Value="@SearchModel.SelectedModulesIDs" Items="@AllSysModules" PlaceHolder="@WtmBlazor.Localizer["Sys.All"]"/>
            <MultiSelect @bind-Value="@SearchModel.SelectedDevicesIDs" Items="@AllSysDevices" PlaceHolder="@WtmBlazor.Localizer["Sys.All"]"/>
            <Select @bind-Value="@SearchModel.IsUpdate" Items="@WtmBlazor.GlobalSelectItems.SearcherBoolItems" PlaceHolder="@WtmBlazor.Localizer["Sys.All"]"></Select>
            <WTDateRange @bind-Value="@SearchModel.LastTime"  />
            <WTDateRange @bind-Value="@SearchModel.LoginTime"  />
        </Row>
    </ValidateForm>
</WTSearchPanel>



<Table @ref="dataTable" 
       TItem="SysClientView" 
       OnQueryAsync="OnSearch" 
       IsPagination="true" 
       IsStriped="true" 
       IsBordered="true" 
       ShowRefresh="false"
       ShowToolbar="true" 
       IsMultipleSelect="true" 
       ShowExtendButtons="true" 
       ShowDefaultButtons="false" 
       ShowExtendEditButton="false"
       ShowExtendDeleteButton="false"
       ShowLoading="true"
       style="margin-top:10px;">
    <TableColumns>
        <TableColumn @bind-Field="@context.Name"  />
        <TableColumn @bind-Field="@context.Code"  />
        <TableColumn @bind-Field="@context.Address"  />
        <TableColumn @bind-Field="@context.Ip"  />
        <TableColumn @bind-Field="@context.MacAddress" Visible="false" />
        <TableColumn @bind-Field="@context.Version"  Sortable="true" />
        <TableColumn @bind-Field="@context.ModuleList"  />
        <TableColumn @bind-Field="@context.DeviceList" Visible="false" />
        <TableColumn @bind-Field="@context.PayList" Visible="false" />
        <TableColumn @bind-Field="@context.CardList" Visible="false" />
        <TableColumn @bind-Field="@context.IsUpdate" ComponentType="@typeof(Switch)" Visible="false" />
        <TableColumn @bind-Field="@context.LastTime" FormatString="yyyy-MM-dd HH:mm:ss" Sortable="true" />
        <TableColumn @bind-Field="@context.LoginTime" FormatString="yyyy-MM-dd HH:mm:ss" Visible="false" />
    </TableColumns>
    <TableToolbarTemplate>
        @if (IsAccessable("/api/SysClient/Add"))
        {
            <TableToolbarButton 
                TItem="SysClientView" 
                Color="Color.Primary" 
                Icon="fa fa-fw fa-plus" 
                Text="@WtmBlazor.Localizer["Sys.Create"]" 
                OnClickCallback="OnCreateClick" />
        }
        @if (IsAccessable("/api/SysClient/BatchDelete"))
        {
            <TableToolbarPopconfirmButton 
                TItem="SysClientView" 
                Color="Color.Primary"
                Icon="fa fa-fw fa-trash-o" 
                Text="@WtmBlazor.Localizer["Sys.BatchDelete"]"
                OnConfirm="@OnBatchDeleteClick" 
                Content="@WtmBlazor.Localizer["Sys.BatchDeleteConfirm"]" 
                CloseButtonText="@WtmBlazor.Localizer["Sys.Close"]"
                ConfirmButtonText="@WtmBlazor.Localizer["Sys.BatchDelete"]" 
                ConfirmButtonColor="Color.Danger" />
        }
        @if (IsAccessable("/api/SysClient/Import"))
        {
            <TableToolbarButton 
                TItem="SysClientView" 
                Color="Color.Primary" 
                Icon="fa fa-fw fa-upload" 
                Text="@WtmBlazor.Localizer["Sys.Import"]" 
                OnClickCallback="@OnImportClick" />
        }
        @if (IsAccessable("/api/SysClient/ExportExcel"))
        {
            <TableToolbarButton 
                TItem="SysClientView"
                Color="Color.Primary" 
                Icon="fa fa-fw fa-download" 
                Text="@WtmBlazor.Localizer["Sys.Export"]"
                OnClickCallback="@OnExportClick" IsAsync="true" />
        }
    </TableToolbarTemplate>
    <RowButtonTemplate>
        <div style="padding-right:10px;">
            <PopConfirmButton 
                Icon="fa fa-refresh" 
                Text="刷新" 
                OnConfirm="() => OnClientActionClick(context, ClientTaskActionEnum.刷新)" 
                Color="Color.Primary" 
                Size="Size.ExtraSmall"
                Content="确定刷新吗?" 
                CloseButtonText="取消" 
                ConfirmButtonText="确认" 
                ConfirmButtonColor="Color.Primary"/>
            <PopConfirmButton 
                Icon="fa fa-refresh" 
                Text="重启" 
                OnConfirm="() => OnClientActionClick(context, ClientTaskActionEnum.重启)" 
                Color="Color.Warning" 
                Size="Size.ExtraSmall"
                Content="确定重启吗?" 
                CloseButtonText="取消" 
                ConfirmButtonText="确认" 
                ConfirmButtonColor="Color.Warning"/>
            <PopConfirmButton 
                Icon="fa fa-window-close-o" 
                Text="关闭客户端" 
                OnConfirm="() => OnClientActionClick(context, ClientTaskActionEnum.关闭)" 
                Color="Color.Danger" 
                Size="Size.ExtraSmall"
                Content="确定关闭客户端吗?" 
                CloseButtonText="取消" 
                ConfirmButtonText="确认" 
                ConfirmButtonColor="Color.Danger"/>
            <PopConfirmButton 
                Icon="fa fa-window-close-o" 
                Text="关机" 
                OnConfirm="() => OnClientActionClick(context, ClientTaskActionEnum.关机)" 
                Color="Color.Danger" 
                Size="Size.ExtraSmall"
                Content="确定关机吗?" 
                CloseButtonText="取消" 
                ConfirmButtonText="确认" 
                ConfirmButtonColor="Color.Danger"/>
            @if (IsAccessable("/api/SysClient/Edit"))
            {
                <TableCellButton Size="Size.ExtraSmall" Color="Color.Success" Icon="fa fa-edit" Text="@WtmBlazor.Localizer["Sys.Edit"]" OnClick="() => OnEditClick(context)" />
            }
            @if (IsAccessable("/api/SysClient/{id}"))
            {
                <TableCellButton Size="Size.ExtraSmall" Color="Color.Info" Icon="fa fa-info" Text="@WtmBlazor.Localizer["Sys.Details"]" OnClick="()=>OnDetailsClick(context)" />
            }
            @if (IsAccessable("/api/SysClient/BatchDelete"))
            {
                <TableCellButton Icon="fa fa-trash-o" Text="@WtmBlazor.Localizer["Sys.Delete"]" OnConfirm="() => OnDeleteClick(context)" Color="Color.Danger" Size="Size.ExtraSmall"
                                  Content="@WtmBlazor.Localizer["Sys.DeleteConfirm"]" CloseButtonText="@WtmBlazor.Localizer["Sys.Close"]" ConfirmButtonText="@WtmBlazor.Localizer["Sys.Delete"]" ConfirmButtonColor="Color.Danger" />
            }
        </div>
    </RowButtonTemplate>
</Table>

@code{

    private SysClientSearcher SearchModel = new SysClientSearcher();
    private Table<SysClientView> dataTable;

    private List<SelectedItem> AllSysModules = new List<SelectedItem>();

    private List<SelectedItem> AllSysDevices = new List<SelectedItem>();


    protected override async Task OnInitializedAsync()
    {

        AllSysModules = await WtmBlazor.Api.CallItemsApi("/api/SysClient/GetSysModules");

        AllSysDevices = await WtmBlazor.Api.CallItemsApi("/api/SysClient/GetSysDevices");

        await base.OnInitializedAsync();
    }

    private async Task<QueryData<SysClientView>> OnSearch(QueryPageOptions opts)
    {
        return await StartSearch<SysClientView>("/api/SysClient/Search", SearchModel, opts);
    }

    private void DoSearch()
    {
        dataTable.QueryAsync();
    }

    private async Task OnCreateClick(IEnumerable<SysClientView> items)
    {
        if (await OpenDialog<Create>(WtmBlazor.Localizer["Sys.Create"]) == DialogResult.Yes)
        {
            await dataTable.QueryAsync();
        }
    }

    private async Task OnEditClick(SysClientView item)
    {
        if (await OpenDialog<Edit>(WtmBlazor.Localizer["Sys.Edit"], x => x.id == item.ID.ToString()) == DialogResult.Yes)
        {
            await dataTable.QueryAsync();
        }
    }

    private async Task OnDetailsClick(SysClientView item)
    {
        await OpenDialog<Details>(WtmBlazor.Localizer["Sys.Details"], x => x.id == item.ID.ToString());
    }

    private async Task OnBatchDeleteClick()
    {
        if (dataTable.SelectedRows?.Any() == true)
        {
            await PostsData(dataTable.SelectedRows.Select(x => x.ID).ToList(), $"/api/SysClient/batchdelete", (s) => WtmBlazor.Localizer["Sys.BatchDeleteSuccess", s]);
            await dataTable.QueryAsync();
        }
        else
        {
            await WtmBlazor.Toast.Information(WtmBlazor.Localizer["Sys.Info"], WtmBlazor.Localizer["Sys.SelectOneRowMin"]);
        }
    }

    private async Task OnDeleteClick(SysClientView item)
    {
        await PostsData(new List<string> { item.ID.ToString() }, $"/api/SysClient/batchdelete", (s) => "Sys.OprationSuccess");
        await dataTable.QueryAsync();
    }


    private async Task OnClientActionClick(SysClientView item, ClientTaskActionEnum action)
    {
        var vm = new SysClientTaskVm();
        vm.Entity.ClientId = item.ID;
        vm.Entity.Action = action;
        await PostsData(vm, $"/api/SysClientTask/add", (s) => "Sys.OprationSuccess");
        await dataTable.QueryAsync();
    }
    
    private async Task OnExportClick(IEnumerable<SysClientView> items)
    {
        if (dataTable.SelectedRows?.Any() == true)
        {
            await Download("/api/SysClient/ExportExcelByIds", dataTable.SelectedRows.Select(x => x.ID.ToString()).ToList());
        }
        else
        {
            await Download("/api/SysClient/ExportExcel", SearchModel);
        }
    }
    private async Task OnImportClick(IEnumerable<SysClientView> items)
    {
        if (await OpenDialog<Import>(WtmBlazor.Localizer["Sys.Import"]) == DialogResult.Yes)
        {
            await dataTable.QueryAsync();
        }
    }
}
